package easytasks.logic;

import java.util.Calendar;
import java.util.Date;

import easytasks.main.R;

import android.content.Context;

public class DateFormatter 
{	
	private Context context;
	
	public DateFormatter(Context context)
	{
		if (context == null) throw new IllegalArgumentException("'context' must be not null");
		this.context = context;
	}
	
	public String getTimeAsString(Date date, DateAsStringType dateType, boolean ignoreHour)
	{
		if (date == null) throw new IllegalArgumentException("'date' must be not null");
		
		String timeAsString = "";		
		Calendar calendar = Calendar.getInstance();	
		calendar.setTime(date);
		
		switch (dateType) 
		{
			case WEEKDAYDAY_AND_LONGMONTHANDHOUR:
				int currentYear = Calendar.getInstance().get(Calendar.YEAR);
				
				if (ignoreHour) 
				{
					timeAsString = context.getString(R.string.dateformatter_date_longmonthandhour_nohour);
				}
				else 
				{
					timeAsString = context.getString(R.string.dateformatter_date_longmonthandhour_hour);
					timeAsString = timeAsString.replace("#4#", getHourAsString(calendar));
				}
			
				timeAsString = timeAsString.replace("#0#", getWeekDayString(calendar));
				timeAsString = timeAsString.replace("#1#", getMonthAsLongString(calendar));
				timeAsString = timeAsString.replace("#2#", String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)));
				
				if (currentYear != calendar.get(Calendar.YEAR)) timeAsString = timeAsString.replace("#3#", ", " + calendar.get(Calendar.YEAR));
				else timeAsString = timeAsString.replace("#3#", "");
				
				break;
			case WEEKDAY_AND_SHORTMONTHANDHOUR:
				if (ignoreHour)
				{
					timeAsString = context.getString(R.string.dateformatter_date_shortmonthandhour_nohour);
				}
				else
				{
					timeAsString = context.getString(R.string.dateformatter_date_shortmonthandhour_hour);
					timeAsString = timeAsString.replace("#4#", getHourAsString(calendar));
				}
				
				timeAsString = timeAsString.replace("#0#", getWeekDayString(calendar));
				timeAsString = timeAsString.replace("#1#", getMonthAsShortString(calendar));
				timeAsString = timeAsString.replace("#2#", String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)));
				timeAsString = timeAsString.replace("#3#", ", " + calendar.get(Calendar.YEAR));
				
				break;
			case ONLY_HOUR:
				if (!ignoreHour) 
				{
					timeAsString = context.getString(R.string.dateformatter_date_onlyhour);
					timeAsString = timeAsString.replace("#0#", getHourAsString(calendar));
				}
				
				break;
		}
		
		return timeAsString;
	}

	private String getWeekDayString(Calendar calendar)
	{
		if (calendar == null) throw new IllegalArgumentException("'calendar' must be not null");
		String weekDayString = "";
		
		switch(calendar.get(Calendar.DAY_OF_WEEK))
		{
			case Calendar.SUNDAY:
				weekDayString += context.getString(R.string.dateformatter_sunday);
				break;
			case Calendar.MONDAY:
				weekDayString += context.getString(R.string.dateformatter_monday);
				break;
			case Calendar.TUESDAY:
				weekDayString += context.getString(R.string.dateformatter_tuesday);
				break;
			case Calendar.WEDNESDAY:
				weekDayString += context.getString(R.string.dateformatter_wednesday);
				break;
			case Calendar.THURSDAY:
				weekDayString += context.getString(R.string.dateformatter_thursday);
				break;
			case Calendar.FRIDAY:
				weekDayString += context.getString(R.string.dateformatter_friday);
				break;
			case Calendar.SATURDAY:
				weekDayString += context.getString(R.string.dateformatter_saturday);
				break;
		}
		
		return weekDayString;
	}
	
	private String getMonthAsLongString(Calendar calendar)
	{
		if (calendar == null) throw new IllegalArgumentException("'calendar' must be not null");
		String monthAsString = "";
		
		switch (calendar.get(Calendar.MONTH))
		{
			case Calendar.JANUARY:
				monthAsString = context.getString(R.string.dateformatter_january_1);
				break;
			case Calendar.FEBRUARY:
				monthAsString = context.getString(R.string.dateformatter_february_1);
				break;
			case Calendar.MARCH:
				monthAsString = context.getString(R.string.dateformatter_march_1);
				break;
			case Calendar.APRIL:
				monthAsString = context.getString(R.string.dateformatter_april_1);
				break;
			case Calendar.MAY:
				monthAsString = context.getString(R.string.dateformatter_may_1);
				break;
			case Calendar.JUNE:
				monthAsString = context.getString(R.string.dateformatter_june_1);
				break;
			case Calendar.JULY:
				monthAsString = context.getString(R.string.dateformatter_july_1);
				break;
			case Calendar.AUGUST:
				monthAsString = context.getString(R.string.dateformatter_august_1);
				break;
			case Calendar.SEPTEMBER:
				monthAsString = context.getString(R.string.dateformatter_september_1);
				break;
			case Calendar.OCTOBER:
				monthAsString = context.getString(R.string.dateformatter_october_1);
				break;
			case Calendar.NOVEMBER:
				monthAsString = context.getString(R.string.dateformatter_november_1);
				break;
			case Calendar.DECEMBER:
				monthAsString = context.getString(R.string.dateformatter_december_1);
				break;
		}
		
		return monthAsString;
	}
	
	private String getMonthAsShortString(Calendar calendar)
	{
		if (calendar == null) throw new IllegalArgumentException("'calendar' must be not null");
		String monthAsString = "";
		
		switch (calendar.get(Calendar.MONTH))
		{
			case Calendar.JANUARY:
				monthAsString = context.getString(R.string.dateformatter_january_2);
				break;
			case Calendar.FEBRUARY:
				monthAsString = context.getString(R.string.dateformatter_february_2);
				break;
			case Calendar.MARCH:
				monthAsString = context.getString(R.string.dateformatter_march_2);
				break;
			case Calendar.APRIL:
				monthAsString = context.getString(R.string.dateformatter_april_2);
				break;
			case Calendar.MAY:
				monthAsString = context.getString(R.string.dateformatter_may_2);
				break;
			case Calendar.JUNE:
				monthAsString = context.getString(R.string.dateformatter_june_2);
				break;
			case Calendar.JULY:
				monthAsString = context.getString(R.string.dateformatter_july_2);
				break;
			case Calendar.AUGUST:
				monthAsString = context.getString(R.string.dateformatter_august_2);
				break;
			case Calendar.SEPTEMBER:
				monthAsString = context.getString(R.string.dateformatter_september_2);
				break;
			case Calendar.OCTOBER:
				monthAsString = context.getString(R.string.dateformatter_october_2);
				break;
			case Calendar.NOVEMBER:
				monthAsString = context.getString(R.string.dateformatter_november_2);
				break;
			case Calendar.DECEMBER:
				monthAsString = context.getString(R.string.dateformatter_december_2);
				break;
		}
		
		return monthAsString;
	}
	
	private String getHourAsString(Calendar calendar)
	{
		if (calendar == null) throw new IllegalArgumentException("'calendar' must be not null");
		
		String hour = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
		
		String minute = String.valueOf(calendar.get(Calendar.MINUTE));
		if (minute.length() == 1) minute = "0" + minute;
		
		return (hour + ":" + minute);
	}
	
	public enum DateAsStringType
	{
		WEEKDAYDAY_AND_LONGMONTHANDHOUR, WEEKDAY_AND_SHORTMONTHANDHOUR, ONLY_HOUR;
	}
}
